var getNext = function (num) {
  let result = 0;
  while (num) {
    result += (num % 10) * (num % 10);
    num = Math.floor(num / 10);
  }
  return result;
}
var isHappy = function (n) {
  let pre = n, cur = getNext(n);
  while (cur !== 1) {
    pre = getNext(pre);
    cur = getNext(getNext(cur));
    if(cur === pre){
      return false;
    }
  }
  return true;
};

let arr = [];
for(let i = 0; i< 100001; i++){
  if(isHappy(i)){
    arr.push(i)
  }
}

let sum = 0;
arr.forEach(i=>{
  sum += i;
})
// let sum = arr.reduce((pre, cur)=>{
//   pre += cur;
//   return pre;
// }, 0);
console.log(sum);